package com.briup.jz.service.impl;

import com.briup.jz.bean.Order;
import com.briup.jz.bean.OrderExample;
import com.briup.jz.bean.OrderLine;
import com.briup.jz.bean.User;
import com.briup.jz.bean.extend.OrderExtend;
import com.briup.jz.dao.OrderLineMapper;
import com.briup.jz.dao.OrderMapper;
import com.briup.jz.dao.UserMapper;
import com.briup.jz.dao.extend.OrderExtendMapper;
import com.briup.jz.service.IOrderService;
import com.briup.jz.utils.CustomerException;
import com.briup.jz.vm.OrderVM;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @program: jz
 * @description: 订单业务实现类
 * @author: 罗宇
 * @create: 2020-06-13 11:57
 **/
@Service
public class OrderServiceImpl implements IOrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderExtendMapper orderExtendMapper;
    @Resource
    private OrderLineMapper orderLineMapper;
    @Resource
    private UserMapper userMapper;
    //派单
    @Override
    public void updateOrderEmployeeById(long id, long employee) throws CustomerException{

        orderExtendMapper.updateOrderEmployeeById(id, employee);
    }

    @Override
    public void commit(OrderVM orderVM) throws CustomerException {
        /*
        * 1. 分别获取到订单信息 及订单项信息
        * 2. 先保存订单信息
        * 3. 保存订单项信息
        * */
        Order order = new Order();
        order.setTotal(orderVM.getTotal());
        order.setAddressId(orderVM.getAddressId());
        order.setCustomerId(orderVM.getCustomerId());
        order.setOrderTime(new Date().getTime());
        order.setStatus(OrderExtend.STATUS_WFK);

        List<OrderLine> orderLines = orderVM.getOrderLines();

        orderMapper.insert(order);

        for(OrderLine orderLine : orderLines){
            // 设置order与orderline之间的关键（外键维护）
            orderLine.setOrderId(order.getId());
            orderLineMapper.insert(orderLine);
        }
    }

    @Override
    public List<OrderExtend> findAll() {
        return orderExtendMapper.selectAll();
    }

    @Override
    public OrderExtend findOrderDetailsById(long id) {
        return orderExtendMapper.selectById(id);
    }

    @Override
    public List<OrderExtend> findNotpayingByCustomerId(long id){
    return orderExtendMapper.selectNotpayingByCustomerId(id);
    }
    public List<OrderExtend> findAllNotpaying(){
        return orderExtendMapper.selectAllNotpaying();
    }


    @Override
    //根据客户id查询所有已付款订单
    public List<OrderExtend> findPaidByCustomerId(long id) {
        return orderExtendMapper.selectPaidByCustomerId(id);
    }

    @Override
    //查询所有已付款订单
    public List<OrderExtend> findAllPaid() {
        return orderExtendMapper.selectAllPaid();
    }

    @Override
    //根据订单状态和客户id查询所有订单
    public List<OrderExtend> findStatusByCustomerId(String string,long id) {
        return orderExtendMapper.selectStatusByCustomerId(string,id);
    }
    @Override
    //根据订单状态查询所有订单
    public List<OrderExtend> findStatus(String string) {
        return orderExtendMapper.selectStatus(string);
    }

    //修改订单状态
    @Override
    public void updateOrderStatusById(String status, long id) {
         orderExtendMapper.updateOrderStatusById(status, id);
    }

    @Override
    public void sendOrder(long orderId, long employeeId) throws Exception {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        if(order == null ){
            throw new Exception("该订单不存在");
        }else if (!order.getStatus().equals("待派单") ){
            throw new Exception("订单未支付/订单异常");
        }
        User emp = userMapper.selectByPrimaryKey(employeeId);
        if (emp == null){
            throw new Exception("该员工不存在");
        }

    }

}





















